package com.haizhi.crm.sys.shiro;

import org.apache.commons.lang3.StringUtils;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by yangyijun on 18/7/2.
 */
public class ChainDefinitionProvider {

    @Transactional
    public Map<String, String> getAllRolesPermissions() {

        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
//        List<com.haizhi.crm.common.entity.Resource> resources = resourceRepo.findAllByType("api");
        HashMap<String, Set<String>> apiRole = new LinkedHashMap<>();

        //TODO 增加通用的权限
//        for (com.haizhi.crm.common.entity.Resource resource : resources) {
//            List<Position> relateRoles = resource.getRoles();
//            String api = resource.getApi().trim().replace(" ", "");
//            if (!apiRole.containsKey(api)) {
//                apiRole.put(api, new HashSet<>());
//            }
//            Set<String> roles = apiRole.get(api);
//            if (relateRoles != null) {
//                for (Position role : relateRoles) {
//                    roles.add(role.getId().toString());
//                }
//            }
//        }

        for (Map.Entry<String, Set<String>> entity : apiRole.entrySet()) {
            String api = entity.getKey();
            String roles = StringUtils.join(entity.getValue(), "," );
            filterChainDefinitionMap.put(api, "roles[" + roles + "]" );
        }


        /**
         * 对swagger 不设权限
         */
        filterChainDefinitionMap.put("/swagger-ui.html", "anon" );
        filterChainDefinitionMap.put("/swagger-ui.html#/**", "anon" );
        filterChainDefinitionMap.put("/swagger-resources/**", "anon" );
        filterChainDefinitionMap.put("/v2/api-docs", "anon" );
        filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon" );

        filterChainDefinitionMap.put("/auth/login", "anon" );
        filterChainDefinitionMap.put("/browseCount/hotSearchWords", "anon" );
        filterChainDefinitionMap.put("/search/suggest", "anon" );
        filterChainDefinitionMap.put("/**", "authc" );

        //TODO 提供一些额外的控制
        return filterChainDefinitionMap;
    }
}
